package com.pro.config;

import com.pro.entity.User;
import com.pro.service.LoginService;
import com.pro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * ClassName:SecurityService
 * Package:com.pro.security
 * Description:
 *
 * @Date:2021/11/6 17:38
 * @Author: LBF
 */
@Component
public class SecurityServiceImple implements UserDetailsService {

    @Autowired
    private LoginService loginService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = loginService.getUser(username);
        //System.out.println("用户"+user);
        if (user == null){
            throw new UsernameNotFoundException("用户名错误");
        }
        //三个参数为，用户名、用户密码、用户权限集合(没有权限，创建一个空的集合)
        org.springframework.security.core.userdetails.User result = new org.springframework.security.core.userdetails.User(
                username,user.getPassword(),AuthorityUtils.createAuthorityList()
        );
        //System.out.println("认证");
        return result;
    }



}
